<template>
  <div class="components-wallet-withdraw">
    <div class="search-bar">
      <el-form
        ref="searchFormRef"
        :inline="true"
        class="search-form"
        :model="searchForm"
        @submit.prevent
      >
        <div class="input-row">
          <el-form-item
            prop="nickName"
            :label="$t('distributionMsg.nickName')+':'"
          >
            <el-input
              v-model="searchForm.nickName"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.nickName')"
            />
          </el-form-item>
          <el-form-item
            prop="userMobile"
            :label="$t('distributionMsg.distriTelPhone')+':'"
          >
            <el-input
              v-model="searchForm.userMobile"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.distriTelPhone')"
            />
          </el-form-item>
          <el-form-item
            prop="merchantOrderId"
            :label="$t('distributionMsg.serialNumber')+':'"
          >
            <el-input
              v-model="searchForm.merchantOrderId"
              type="text"
              clearable
              :placeholder="$t('distributionMsg.serialNumber')"
            />
          </el-form-item>
          <el-form-item
            prop="state"
            :label="$t('distributionMsg.withdrawalStatus')+':'"
          >
            <el-select
              v-model="searchForm.state"
              clearable
              :placeholder="$t('distributionMsg.withdrawalStatus')"
            >
              <el-option
                :label="$t('distributionMsg.applying')"
                value="0"
              />
              <el-option
                :label="$t('distributionMsg.withdrawalSuccessful')"
                value="1"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div
              class="default-btn primary-btn"
              @click="onSearch(true)"
            >
              {{ $t('crud.searchBtn') }}
            </div>
            <div
              class="default-btn"
              @click="onResetForm"
            >
              {{ $t('crud.emptyBtn') }}
            </div>
          </el-form-item>
        </div>
      </el-form>
    </div>
    <div class="main-container">
      <div class="table-con">
        <el-table
          ref="noticeListTableRef"
          :data="dataList"
          header-cell-class-name="table-header"
          row-class-name="table-row-low"
          style="width: 100%"
          @sort-change="onSortChange"
        >
          <el-table-column
            width="120"
            :label="$t('distribUserWallet.distributorNickname')"
            prop="nickName"
          >
            <template #default="scope">
              {{
                scope.row.distributionUser.nickName
              }}
            </template>
          </el-table-column>
          <el-table-column
            width="120"
            :label="$t('distributionMsg.distriTelPhone')"
            prop="userMobile"
          >
            <template #default="scope">
              {{
                scope.row.distributionUser.userMobile
              }}
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionMsg.serialNumber')"
            prop="merchantOrderId"
            width="180"
          />
          <el-table-column
            :label="$t('home.amount')"
            prop="amount"
            width="120"
            sortable
          />
          <el-table-column
            width="150"
            :label="$t('distributionMsg.withdrawalTime')"
            prop="createTime"
            sortable
          />
          <el-table-column
            :label="$t('distributionMsg.withdrawalType')"
            prop="moneyFlow"
          >
            <template #default="scope">
              <span v-if="scope.row.moneyFlow===0">{{ $t('distributionMsg.manualWithdrawal') }}</span>
              <span v-else-if="scope.row.moneyFlow===1">{{ $t('distributionMsg.automaticWithdrawal') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            :label="$t('distributionMsg.withdrawalStatus')"
            prop="state"
          >
            <template #default="scope">
              <span v-if="scope.row.state===0">{{ $t('distributionMsg.applying') }}</span>
              <span v-else-if="scope.row.state===1">{{ $t('distributionMsg.withdrawalSuccessful') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            width="150"
            :label="$t('home.updateTime')"
            prop="updateTime"
          >
            <template #default="scope">
              <span v-if="scope.row.updateTime">{{ scope.row.updateTime }}</span>
              <span v-else>{{ $t('distribUserWallet.nothing') }}</span>
            </template>
          </el-table-column>
          <el-table-column
            align="center"
            :label="$t('crud.menu')"
          >
            <template #default="scope">
              <div
                v-if="scope.row.state === 0"
                class="default-btn text-btn"
                @click="updateToSuccess(scope.row.withdrawCashId)"
              >
                {{ $t("distribUserWallet.setWithdrawalSuccess") }}
              </div>
              <div v-else>
                {{ $t('distribUserWallet.nothing') }}
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-pagination
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        :total="page.total"
        layout="total, sizes, prev, pager, next, jumper"
        @update:page-size="onPageSizeChange"
        @update:current-page="onPageChange"
      />
    </div>
  </div>
</template>
<script setup>
import { ElMessageBox, ElMessage } from 'element-plus'

let tempSearchForm = null // 保存上次点击查询的请求条件

const page = reactive({
  total: 0, // 总页数
  currentPage: 1, // 当前页数
  pageSize: 10 // 每页显示多少条
})
const searchForm = reactive({
  userMobile: '',
  merchantOrderId: '',
  state: ''
})
const dataList = ref([])
let sortParam = 3 // 0无 1金额 2手续费 3提现时间 4更新时间
let sortType = 2 // 0无 1 正序 2倒序

// 获取数据列表
const getDataList = (pageParam, newData = false) => {
  if (newData || !tempSearchForm) {
    tempSearchForm = JSON.parse(JSON.stringify(searchForm))
  }
  http({
    url: http.adornUrl('/distribution/distributionWithdrawCash/page/anduser'),
    method: 'get',
    params: http.adornParams(Object.assign(tempSearchForm, {
      current: pageParam ? pageParam.currentPage : 1,
      size: pageParam ? pageParam.pageSize : 10,
      sortParam,
      sortType
    }))
  }).then(({ data }) => {
    page.total = data.total
    page.pageSize = data.size
    page.currentPage = data.current

    dataList.value = data.records
  })
}
onMounted(() => {
  getDataList()
})

const onSortChange = (data) => {
  // 排序字段 0无 1金额 2手续费 3提现时间 4更新时间
  switch (data.prop) {
    case 'amount': sortParam = 1
      break
    case 'fee': sortParam = 2
      break
    case 'createTime': sortParam = 3
      break
    case 'updateTime': sortParam = 4
      break
    default: sortParam = 0
      break
  }
  // 排序类型 0无 1 正序 2倒序
  switch (data.order) {
    case 'descending': sortType = 2
      break
    case 'ascending': sortType = 1
      break
    default: sortType = 0
      break
  }
  getDataList(page)
}
// 新增 / 修改
const updateToSuccess = (withdrawCashId) => {
  ElMessageBox.confirm($t('distribUserWallet.confirmSetWithdrawal'), $t('remindPop.remind'), {
    confirmButtonText: $t('remindPop.confirm'),
    cancelButtonText: $t('remindPop.cancel'),
    type: 'warning'
  }).then(() => {
    http({
      url: http.adornUrl('/distribution/distributionWithdrawCash/toSuccess/' + withdrawCashId),
      method: 'put'
    }).then(() => {
      ElMessage({
        message: $t('publics.operation'),
        type: 'success',
        duration: 1500,
        onClose: () => {
          getDataList()
        }
      })
    })
  }).catch(() => {})
}

// 点击查询
const onSearch = (newData = false) => {
  page.currentPage = 1
  getDataList(page, newData)
}
const onPageSizeChange = (val) => {
  page.pageSize = val
  getDataList(page)
}
const onPageChange = (val) => {
  page.currentPage = val
  getDataList(page)
}

// 重置搜索表单
const searchFormRef = ref(null)
const onResetForm = () => {
  searchFormRef.value.resetFields()
}

</script>
